package com.example.bookstore.beans;

import java.util.Map;

import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.faces.bean.ManagedBean;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import com.example.bookstore.cart.ShoppingCart;
import com.example.bookstore.cart.ShoppingCartItem;
import com.example.bookstore.controllers.BookController;
import com.example.bookstore.controllers.CustomerController;
import com.example.bookstore.details.CustomerDetails;
import com.example.bookstore.entity.Book;
import com.example.bookstore.entity.Customer;

@ManagedBean(name = "base")
public class BaseBean {

	private Customer customer;

	protected BookController bookController() {
		FacesContext context = FacesContext.getCurrentInstance();
		ELContext elContext = context.getELContext();
		ValueExpression ve = context
				.getApplication()
				.getExpressionFactory()
				.createValueExpression(elContext, "#{bookCtrl}",
						BookController.class);

		return (BookController) ve.getValue(elContext);
	}

	protected CustomerController customerController() {
		FacesContext context = FacesContext.getCurrentInstance();
		ELContext elContext = context.getELContext();
		ValueExpression ve = context
				.getApplication()
				.getExpressionFactory()
				.createValueExpression(elContext, "#{custCtrl}",
						CustomerController.class);

		return (CustomerController) ve.getValue(elContext);
	}

	protected ShoppingCart shoppingCart() {
		FacesContext context = FacesContext.getCurrentInstance();
		ELContext elContext = context.getELContext();
		ValueExpression ve = context
				.getApplication()
				.getExpressionFactory()
				.createValueExpression(elContext, "#{cart}", ShoppingCart.class);

		return (ShoppingCart) ve.getValue(elContext);
	}

	public String catalog() {
		return "catalog";
	}

	public String details() {
		return "details";
	}

	public String cart() {
		return "cart";
	}

	public String checkout() {
		return "checkout";
	}

	public String account() {
		return "account";
	}

	public String login() {
		return "login";
	}

	public String profile() {
		return "profile";
	}

	public String orders() {
		return "orders";
	}

	public String search() {
		return "search";
	}

	public String logout() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		ExternalContext externalContext = facesContext.getExternalContext();
		externalContext.invalidateSession();
		return "logout";
	}

	public String buy() {
		return "buy";
	}

	protected Map<String, Object> getRequestMap() {
		return FacesContext.getCurrentInstance().getExternalContext()
				.getRequestMap();
	}

	protected Map<String, Object> getSessionMap() {
		return FacesContext.getCurrentInstance().getExternalContext()
				.getSessionMap();
	}

	protected Book selected() {
		return (Book) getSessionMap().get("selected");
	}

	protected ShoppingCartItem item() {
		return (ShoppingCartItem) getRequestMap().get("item");
	}

	public String clear() {
		shoppingCart().clear();

		return "";
	}

	protected CustomerDetails customer() {
		return (CustomerDetails) getSessionMap().get("customer");
	}
}
